#nullable enable

using System;

namespace PHMEE.CommonUtils;

public static class DateTimeUtils
{
    /// <summary>
    /// 将 DateTime Kind 设置为 UTC 时间，但不改变时间的值
    /// 例如：2021-08-01 12:00:00 本地时间，调用该方法后，依然是 2021-08-01 12:00:00，但 DateTime 的 Kind 已经转为了 Utc
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static DateTime ToUtcKind(this DateTime dateTime)
    {
        return DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
    }
    
    /// <summary>
    /// 将 DateTime 转换为 UTC 时间的字符串
    /// 例如：DateTime 2021-08-01 12:00:00 转换为 "2021-08-01T12:00:00.000Z"
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static string ToFormattedUtcString(this DateTime dateTime)
    {
        return dateTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
    }
    
    /// <summary>
    /// 根据 DateOnly 和 TimeOnly 获取 UTC DateTime，精确到分钟，秒置 0
    /// </summary>
    /// <param name="dateOnly"></param>
    /// <param name="timeOnly"></param>
    /// <returns></returns>
    public static DateTime GetUtcDateTimeByDateOnlyTimeOnlyUpToMinute(DateOnly dateOnly, TimeOnly timeOnly)
    {
        return new DateTime(dateOnly.Year, dateOnly.Month, dateOnly.Day, timeOnly.Hour, timeOnly.Minute, 0, DateTimeKind.Utc);
    }
     
    /// <summary>
    /// 将输入的 TimeOnly 只精确到分钟
    /// </summary>
    /// <param name="timeOnly"></param>
    /// <returns></returns>
    public static TimeOnly GetTimeOnlyUpToMinute(TimeOnly timeOnly)
    {
        return new TimeOnly(timeOnly.Hour, timeOnly.Minute);
    }
}